<template>
  <div class="template">
    <div class="template-items">
      <div
        class="template-item"
        v-for="(item, index) in data"
        :key="index"
        :class="{
          'is-active': active === index
        }"
        @click="changeActive(index, item)"
      >
        <div class="template-title">{{item.templateStyle}}</div>
        <div>{{ `${item.imageWidth}x${item.imageHeight}` }}</div>
      </div>
      <Tip v-show="!data.length">没有创意模板</Tip>
    </div>
    <div
      v-show="active !== -1"
      class="template-preview"
    >
      <img
        v-if="templateItem.templatePreview"
        class="template-preview__img"
        :src="templateItem.templatePreview"
        :alt="templateItem.templateStyle"
      >
      <div class="c-hint-warn u-prewrap">
        <div class="u-bb-major u-bold">所属单元选中广告位（{{templateItem.applyAdspace.length}}个）</div>
        <div class="template-preview__text">{{templateItem.applyAdspace.length ? templateItem.applyAdspace.join("\n") : "无"}}</div>
      </div>
    </div>
  </div>
</template>

<script>
import Tip from "@/components/tip";
export default {
  name: "CreativeTemplate",
  components: {
    Tip
  },
  props: ["data", "template-id", "disabled"],
  data() {
    return {
      active: -1,
      templateItem: {
        templatePreview: "",
        templateStyle: "",
        applyAdspace: []
      }
    };
  },
  watch: {
    data: {
      deep: true,
      handler(v) {
        if (v) {
          // 默认
          let active = -1;
          v.forEach((item, index) => {
            if (item.templateId === this.templateId) {
              this.templateItem = item;
              active = index;
            }
          });
          this.active = active;
          if (active !== -1) {
            this.$emit("set-template", v[active]);
          }
        }
      }
    }
  },
  methods: {
    changeActive(index, item) {
      if (this.disabled) return false;
      this.templateItem = item;
      this.active = index;
      this.$emit("change", item);
    }
  }
};
</script>

<style lang="scss" scoped>
@import "~@/assets/scss/color";
.template {
  position: relative;
  &-title {
    height: 48px;
    line-height: 24px;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
  }
  &-items {
    overflow: hidden;
    margin-bottom: -10px;
    margin-right: -10px;
  }
  &-item {
    list-style: none;
    float: left;
    width: 120px;
    line-height: 24px;
    text-align: center;
    padding: 6px;
    border: 1px solid #e6e6e6;
    margin: 0 0 10px;
    background: #fff;
    cursor: pointer;
    &.is-active {
      border-color: #04c1fe;
    }
    &:not(:last-child) {
      margin-right: 10px;
    }
  }
  &-preview {
    min-width: 190px;
    padding: 10px;
    position: absolute;
    z-index: 10;
    top: -15px;
    left: 600px;
    border: 1px solid $--border-major;
    &::before,
    &::after {
      content: " ";
      position: absolute;
      display: block;
      width: 0;
      height: 0;
      border-color: transparent;
      border-style: solid;
    }
    &::before {
      top: 20px;
      left: -10px;
      border-width: 10px;
      border-left-width: 0;
      border-right-color: $--border-major;
    }
    &::after {
      top: 22px;
      left: -8px;
      border-width: 8px;
      border-left-width: 0;
      border-right-color: $--background-high;
    }
    &__img {
      max-width: 320px;
      max-height: 320px;
      display: block;
    }
    &__text {
      max-height: 200px;
      overflow: auto;
    }
  }
}
</style>